package com.mm.cloud.sys.auth.controller;

import com.mm.cloud.common.core.result.Result;
import com.mm.cloud.common.core.result.ResultUtil;
import com.mm.cloud.common.security.annotation.ParameterCheck;
import com.mm.cloud.common.security.helper.LoginUserHelper;
import com.mm.cloud.common.security.loginuser.LoginToken;
import com.mm.cloud.common.security.loginuser.LoginUser;
import com.mm.cloud.sys.auth.core.provider.ProviderManager;
import com.mm.cloud.sys.auth.core.token.UsernamePasswordAuthenticateToken;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * 登录相关接口
 *
 * @author mr
 * @since 2021-06-09
 */
@Tag(name = "登录接口")
@Slf4j
@AllArgsConstructor
@RestController
public class LoginController {

    /**
     * 认证服务
     */
    private final ProviderManager providerManager;

    @Operation(summary = "用户名密码登录【mr】", description = "用户名密码登录")
    @PostMapping("/login")
    @ParameterCheck
    public Result<LoginToken> login(@RequestBody UsernamePasswordAuthenticateToken authenticateToken) {
        return ResultUtil.success(providerManager.authenticate(authenticateToken));
    }

    @Operation(summary = "获取登录信息【mr】", description = "获取登录信息")
    @PostMapping("/currentUser")
    public Result<LoginUser> currentUser() {
        return ResultUtil.success(LoginUserHelper.getLoginUser());
    }

    @Operation(summary = "注销【mr】", description = "注销")
    @PostMapping("/logout")
    public Result<String> logout() {
        providerManager.logout();
        return ResultUtil.success();
    }

}
